package design.simdi.code.final_paper.core.entities;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Index;

import design.simdi.code.final_paper.core.enums.UserStatus;

/**
 * 
 * @author Marius Dumitriu - (May 21, 2010 - 1:18:55 PM)
 * 
 */
@Entity
@Table(name = "users")
@AttributeOverrides( { @AttributeOverride(name = "id", column = @Column(name = "User_Id")) })
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class User extends ApplicationEntity {

	private static final long serialVersionUID = 8445828134021889255L;

	private String login;
	private String password;
	private String email;
	private Collection<UserRole> userRoles;
	private UserStatus status;

	public User() {
	}

	/**
	 * @return the login
	 */
	@Column(length = 100, nullable = false, unique = true, name = "Login")
	@Index(name = "Login")
	public String getLogin() {
		return login;
	}

	/**
	 * @param login
	 *            the login to set
	 */
	public void setLogin(String login) {
		this.login = login;
	}

	/**
	 * @return the password
	 */

	@Column(length = 100, nullable = false, name = "Password")
	@Index(name = "Password")
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the email
	 */
	@Column(length = 100, nullable = false, name = "Email")
	@Index(name = "Email")
	public String getEmail() {
		return email;
	}

	/**
	 * @param email
	 *            the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(login);
		return sb.toString();
	}

	/**
	 * @param userGroups
	 *            the userGroups to set
	 */
	public void setUserRoles(Collection<UserRole> userRoles) {
		this.userRoles = userRoles;
	}

	/**
	 * @return the userGroups
	 */
	@OneToMany(mappedBy = "user")
	public Collection<UserRole> getUserRoles() {
		return userRoles;
	}

	@Transient
	public List<Role> getRoles() {
		Collection<UserRole> colectieUserRole = getUserRoles();
		List<Role> listaRoluri = new ArrayList<Role>();

		if (colectieUserRole == null)
			return listaRoluri;

		for (UserRole ur : colectieUserRole) {
			listaRoluri.add(ur.getRole());
		}
		return listaRoluri;
	}

	public void setStatus(UserStatus status) {
		this.status = status;
	}

	@Column(nullable = false, name = "Status")
	public UserStatus getStatus() {
		return status;
	}
}
